package com.itheima.controller;


import com.itheima.pojo.*;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //显示所有学生信息
    @GetMapping
    public Result list(StudentQueryParam param) {
        log.info("查询所有学生信息：{}", param);

        //调用Service层查询分页数据
        PageResult<Student> pageResult = studentService.getPageResult(param);
        return Result.success(pageResult);
    }

    //新增学生数据 TODO 新增无法添加学历
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("新增学生信息：{}", student);

        //调用service层保存学生信息
        studentService.saveStu(student);
        return Result.success();
    }

    //数据回显，方便更新学生数据
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("根据id查询学生信息：{}", id);

        Student student = studentService.getStuById(id);
        return Result.success(student);
    }

    //根据回显数据更新数据
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息：{}", student);

        //调用service层修改学生信息
        studentService.updateStu(student);

        return Result.success();
    }

    //删除学生数据
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids) {
        log.info("根据id删除学生信息：{}", ids);
        //调用service层删除学生信息
        studentService.removeStuByIds(ids);

        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("根据id{}扣分, 扣分：{}", id, score);
        //违纪处理
        studentService.violation(id, score);
        return Result.success();
    }
}
